<?PHP
namespace Lib;

/**
 * 框架封装的默认控制器类
 * 提供了错误显示页面，格式化输出等控制器公共方法
 * @package SDF\Controllers
 */
class Controller{
    protected $DATA = array();

    /**
     * 显示404页面
     * @param $msg
     * @throws \Exception
     */
    protected function _show404($msg){
        $this->DATA['msg'] = $msg;
        //\SDF\SDF::set_status_header(404);
        View::display($this->DATA,'_show404');
    }

    /**
     * 显示错误消息
     * @param $msg
     * @param array $option
     * @throws \Exception
     */
    protected function _error($msg, $option = array()) {
        if (IS_AJAX) self::form_json(array(), $msg, 500, $option);
        $this->DATA['msg'] = $msg;
        View::display($this->DATA, '_error');
    }

    /**
     * 显示提醒消息
     * @param $msg
     * @param array $option
     * @throws \Exception
     */
    protected function _info($msg, $option = array()) {
        if(IS_AJAX)self::form_json(array(),$msg,200,$option);
        $this->DATA['msg'] = $msg;
        View::display($this->DATA,'_info');
    }

    /**
     * 显示处理成功页面
     * @param $msg
     * @param array $option
     * @throws \Exception
     */
    protected function _succeed($msg,$option = array()){
        if(IS_AJAX)self::form_json(array(),$msg,200,$option);
        $this->DATA['msg'] = $msg;
        View::display($this->DATA,'_succeed');
    }

    /**
     * 重新向
     * @param $msg
     * @param $url
     * @throws \Exception
     */
    protected function _redirect($msg,$url){
        if(IS_AJAX)self::form_json(array(),$msg,302,['url'=>$url]);
        $this->DATA['msg'] = $msg;
        header('Refresh:0;url='.$url);//如果没有会跳地址，回跳到欢迎页面。
        View::display($this->DATA,'_succeed');
    }
    /**
     * 返回json格式数据
     * @param $data
     * @param string $message
     * @param int $status_code
     * @param array $option
     */
    protected static function form_json($data, $message = 'OK', $status_code = 200, $option = array()) {
        $ajax_data = array(
            'status' => $status_code,
            'msg' => $message,
            'response' => $data,
        );
        if (is_array($option)) {
            $ajax_data += $option;
        }
        $data_json = \json_encode($ajax_data);
        echo $data_json;
        exit;
    }

    /**
     * 模板渲染
     * @param string $tplFile
     * @throws \Exception
     */
    protected function display($tplFile=''){
        View::display($this->DATA,$tplFile?$tplFile:__ACTION__);
    }

    /**
     * 更新数据
     */

    /*    public function updateFieldValue(){
        $field= $_REQUEST['field'];
        $value = trim($_REQUEST['value']);
        $item_id = $_REQUEST['item_id'];
        if (empty($field)){
            $this->_error('$field is empty');
        }
        if (empty($item_id)){
            $this->_error('$item_id is empty');
        }
        if (empty($value)){
            $this->_error('$value is empty');
        }

        $itemModel = new Model();
        $itemModel->updateOne(
            ['item_id'=>$item_id],
            ['$set'=>[
                $field=>$value
            ]]);
        $this->_succeed('ok');
    }*/

}